﻿using System;
using System.Text;
using System.Collections;

namespace mfRCF.Core
{
    public class mfRcfCommandCollection : ArrayList
    {        
        #region CTOR

        public mfRcfCommandCollection() :
            base()
        {

        }
        #endregion

        #region ArrayList-Members
        private void AssertIfPathAlreadyInserted(mfRcfCommand value)
        {
            foreach (mfRcfCommand item in this)
            {
                if (item.Name == value.Name
                    && item.HttpMethod.Equals(value.HttpMethod))
                {
                    throw new Exception(GetType() + ": value '" + value.HttpPath 
                        + "' is already in command-collection");
                }
            }
        }

        private mfRcfCommand AssertIfTypeIsInvalid(mfRcfCommand value)
        {
            var typedValue = value as mfRcfCommand;

            if (typedValue == null)
                throw new Exception(GetType() + ": value '" + value.ToString() + "' is no valid object from type '" + GetType() + "'");

            return typedValue;
        }

        public new mfRcfCommand this[int index]
        {
            get
            {
                return base[index] as mfRcfCommand;
            }
            set
            {
                var typedValue = AssertIfTypeIsInvalid(value);

                base[index] = typedValue;
            }
        }

        public int Add(mfRcfCommand value)
        {
            AssertIfPathAlreadyInserted(value);

            var typedValue = AssertIfTypeIsInvalid(value);
            return base.Add(typedValue);
        }

        public int BinarySearch(mfRcfCommand value, IComparer comparer)
        {
            var typedValue = AssertIfTypeIsInvalid(value);

            return base.BinarySearch(typedValue, comparer);
        }

        public bool Contains(mfRcfCommand value)
        {
            var typedValue = AssertIfTypeIsInvalid(value);

            return base.Contains(typedValue);
        }

        public int IndexOf(mfRcfCommand value)
        {
            var typedValue = AssertIfTypeIsInvalid(value);

            return base.IndexOf(typedValue);
        }

        public int IndexOf(mfRcfCommand value, int startIndex)
        {
            var typedValue = AssertIfTypeIsInvalid(value);

            return base.IndexOf(typedValue, startIndex);
        }

        public int IndexOf(mfRcfCommand value, int startIndex, int count)
        {
            var typedValue = AssertIfTypeIsInvalid(value);

            return base.IndexOf(typedValue, startIndex, count);
        }

        public void Insert(int index, mfRcfCommand value)
        {
            AssertIfPathAlreadyInserted(value);

            var typedValue = AssertIfTypeIsInvalid(value);

            base.Insert(index, typedValue);
        }

        public void Remove(mfRcfCommand obj)
        {
            var typedValue = AssertIfTypeIsInvalid(obj);

            base.Remove(typedValue);
        }

        #endregion
    }
}
